//
//  ShortVideoTableViewCell.swift
//  UUVideo
//
//  Created by Galaxy on 2024/1/31.
//  Copyright © 2024 qykj. All rights reserved.
//

import UIKit
import SJVideoPlayer

class ShortVideoTableViewCell: UITableViewCell {

    public var model:VideoModel = VideoModel() {
        didSet{
            //            let headers = [
            //                "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1 Edg/92.0.4515.107",
            //                "Referer":websiteModelArr[model.webType].webUrlStr
            //            ]
            videoPlayer.stop()
            videoPlayer.presentView.placeholderImageView.kf.setImage(with: URL.init(string: model.picUrl))
            let asset = AVURLAsset.init(url: URL.init(string: model.videoUrl)!, options: nil)
            videoPlayer.urlAsset = SJVideoPlayerURLAsset.init(avAsset: asset, startPosition:0, playModel: SJPlayModel.init())
            videoPlayer.play()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    lazy var videoPlayer: SJVideoPlayer = {
        let playerView = UIView.init()
        contentView.addSubview(playerView)
        playerView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        let player = SJVideoPlayer.init()
        playerView.addSubview(player.view)
        player.view.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        player.defaultEdgeControlLayer.topAdapter.removeItem(forTag: SJEdgeControlLayerTopItem_Back)
        player.defaultEdgeControlLayer.topAdapter.reload()
        player.defaultEdgeControlLayer.automaticallyShowsPictureInPictureItem = false
        player.defaultEdgeControlLayer.centerAdapter.removeItem(forTag: SJEdgeControlLayerCenterItem_Replay)
        player.defaultEdgeControlLayer.centerContainerView.isHidden = true
        player.gestureController.supportedGestureTypes = SJPlayerGestureTypeMask.singleTap
        player.playbackObserver.playbackStatusDidChangeExeBlock = { player in
            if player.timeControlStatus == .paused {
                if player.currentTime == player.duration {
                    // 已播放长度等于总时长，说明播放完毕
                    player.replay()
                }
            }
        }
        player.gestureController.singleTapHandler = { control,location in
            player.isPaused ? player.play() : player.pauseForUser()
        }
        return player
    }()
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
